/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2019-12-31
 * Time: 9:46
 */
#include "LoginWidget.h"
#include "Ui_Login.h"

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),ui(new Ui::LoginWidget){
    ui->setupUi(this);

    myTime = new MyTimeHelper();

    int x = parent->width()/2 - this->width()/2;
    int y = parent->height()/2 - this->height()/2;
    this->move(x,y);
    this->setStyle("../res/css/tabWidget.css");
    ui->login_username_input->setText("123");
    ui->login_passwd_input->setText("123");
    //登录验证
    connect(ui->login_submit,SIGNAL(clicked()),this,SLOT(isLogin()));
}

LoginWidget::~LoginWidget() {
    delete ui;
}

bool LoginWidget::isLogin() {
    QString username = ui->login_username_input->text();
    QString passwd = ui->login_passwd_input->text();

    user.setName(username);
    user.setPasswd(passwd);

    if(userDao.checkUserInfo(user)){    //检查用户名是否存在
        if (userDao.checkPasswdInfo(user)){
            qDebug()<<"login success";
            QMessageBox::information(this,"提示","登录成功",QMessageBox::QMessageBox::Yes);
            emit goMainForm();
            ui->login_passwd_input->clear();
        }else{
            qDebug()<<"passwd don't same";
            QMessageBox::information(this,"提示","密码不正确",QMessageBox::QMessageBox::Yes);
            ui->login_passwd_input->clear();
        }
    } else{
        qDebug()<<"please sign up";
        QMessageBox::information(this,"提示","账号不存在，请先注册",QMessageBox::QMessageBox::Yes);
        ui->login_username_input->clear();
        ui->login_passwd_input->clear();
    }
    return false;
}

void LoginWidget::setStyle(const QString &styleName) {
    QFile file(QString("%1").arg(styleName));
    file.open(QFile::ReadOnly);
    QString css = QLatin1String(file.readAll());
    qApp->setStyleSheet(css);
}


